<?php
// Include the main TCPDF library (search for installation path).
require_once('tcpdf/tcpdf.php');
require_once('tcpdf/tcpdf_autoconfig.php');

// create new PDF document
//$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('DinhThi');
$pdf->SetTitle('HoaDon');
$pdf->SetSubject('Quan ly ban hang');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

/*// set default header data
$pdf->SetHeaderData(null, 0, 'Cửa hàng Nông Ngư Cơ - HẢI', 'Địa chỉ: Ba Chúc, Tri Tôn, An Giang');

// set header and footer fonts
$pdf->setHeaderFont(Array('dejavusans', '', 10));
$pdf->setFooterFont(Array('dejavusans', '', 10));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);*/

// set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// ---------------------------------------------------------

$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);

// add a page
$pdf->AddPage();

// set default font subsetting mode
$pdf->setFontSubsetting(false);

/*$pdf->SetFont('dejavusans', 'B', 12);

$pdf->Write(0, 'CỬA HÀNG NÔNG NGƯ CƠ HẢI', '', 0, 'L', 1, 0, false, false, 0);
$pdf->Ln(4);
$pdf->Write(0, 'Địa chỉ: ' . $this->DiaChi, '', 0, 'L', 1, 0, false, false, 0);
$pdf->Ln(4);
$pdf->Write(0, 'Số điện thoại: ' . $this->SoDienThoai, '', 0, 'L', 1, 0, false, false, 0);

$pdf->Ln(10);*/
$pdf->SetFont('dejavusans');
$tbl = <<<EOD
<table border="0">
    <tr>
        <td style="width: 220px; text-align: center; font-size: 26px; font-weight: bold;">
            HẢI
        </td>
        <td></td>
    </tr>
    <tr>
        <td style="width: 250px; font-size: 14px;">Đ/C: Ba Chúc, Tri Tôn, An Giang<br/>
            ĐT: 0763 87 22 66<br/>
            DĐ: 0944 079 509 - 0944 079 309
        </td>
        <td style="text-align: center; font-size: 16px;"><span style="font-weight: bold; text-decoration: underline; font-size: 20px;">CHUYÊN</span><br/>PHỤ TÙNG NÔNG - NGƯ - CƠ</td>
    </tr>
<table>
EOD;

$pdf->writeHTML($tbl, true, false, false, false, '');


$pdf->SetFont('dejavusans', 'B', 26);

$pdf->Write(0, 'PHIẾU GIAO HÀNG', '', 0, 'C', 1, 0, false, false, 0);

$pdf->Ln(4);

$info = $this->info;

$pdf->SetFont('dejavusans', 'N', 10);

$pdf->Write(0, 'Ngày bán: ' . date('d/m/Y', strtotime($info['thoigian'])), '', 0, 'R', 1, 0, false, false, 0);
$pdf->Ln(1);
$pdf->Write(0, 'Số hóa đơn: ' . $info['id'], '', 0, 'R', 1, 0, false, false, 0);


$pdf->SetFont('dejavusans', 'N', 12);

$pdf->Write(0, 'Tên khách hàng: ' . $info['ten'], '', 0, 'L', 1, 0, false, false, 0);
$pdf->Ln(1);
$pdf->Write(0, 'Địa chỉ: ' . $info['diachi'], '', 0, 'L', 1, 0, false, false, 0);
$pdf->Ln(1);
$pdf->Write(0, 'Số điện thoại: '  . $info['sodienthoai'], '', 0, 'L', 1, 0, false, false, 0);
$pdf->Ln(2);
$pdf->SetFont('dejavusans', '', 10);

$html = '';
$data = $this->data;
$size = count($data);

for ($i = 0; $i < $size; $i++){
    $tenMH = $data[$i]['ten'];
    $maMH   = $data[$i]['mamathang'];
    $giaban = $data[$i]['giaban'];
    $donvi  = $data[$i]['donvitinh'];
    $soluong = $data[$i]['soluong'];
    $thanhtien = $data[$i]['thanhtien'];

    $html .= '<tr>';
    $html .= '<td style="text-align: center;">' . ($i + 1) . '</td>';
    $html .= '<td style="text-align: center;">' . $maMH . '</td>';
    $html .= '<td>' . $tenMH . '</td>';
    $html .= '<td style="text-align: center;">' . $donvi . '</td>';
    $html .= '<td style="text-align: center;">' . $soluong . '</td>';
    $html .= '<td></td>';
    $html .= '</tr>';
}


while ($i < 10){
    $html .= '<tr>';
    $html .= '<td style="text-align: center;">' . ($i + 1) . '</td>';
    $html .= '<td></td>';
    $html .= '<td></td>';
    $html .= '<td></td>';
    $html .= '<td></td>';
    $html .= '<td></td>';
    $html .= '</tr>';
    $i++;
}


$tbl = <<<EOD
<table cellspacing="0" cellpadding="2" border="1">
    <tr>
        <td style="text-align: center; font-weight: bold; width: 5%;">STT</td>
        <td style="text-align: center; font-weight: bold; width: 10%;">Mã MH</td>
        <td style="text-align: center; font-weight: bold; width: 25%;">Tên mặt hàng</td>
        <td style="text-align: center; font-weight: bold; width: 10%;">ĐVT</td>
        <td style="text-align: center; font-weight: bold; width: 15%;">Số lượng</td>
        <td style="text-align: center; font-weight: bold; width: 35%;">Ghi chú</td>
    </tr>
    {$html}
</table>
EOD;

$pdf->writeHTML($tbl, true, false, false, false, '');

$tbl = <<<EOD
<table border="0">
    <tr>
        <td style="text-align: center; font-weight: bold;">Người mua hàng<br/><br/><br/><br/><br/>{$info['ten']}</td>
        <td style="text-align: center; font-weight: bold;">Người bán hàng</td>
    </tr>
<table>
EOD;

$pdf->writeHTML($tbl, true, false, false, false, '');

/*$pdf->SetFont('dejavusans', '', 10);

$pdf->MultiCell(180, 0, "Câu 1: Trường ĐH Công nghệ Thông tin được thành lập năm nào?\n", 0, 'J', 0, 1, '', '', true, 0);

$pdf->Ln(1);

$pdf->Write(0, "A. 2005", '', 0, 'L', 1, 0, false, false, 0);
$pdf->Write(0, "B. 2006", '', 0, 'L', 1, 0, false, false, 0);
$pdf->Write(0, "C. 2007", '', 0, 'L', 1, 0, false, false, 0);
$pdf->Write(0, "D. 2008", '', 0, 'L', 1, 0, false, false, 0);
*/
// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('GiaoHangHoaDon' . $info['id'] . '.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+
